<!DOCTYPE html>
<html lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
<meta name="keywords" content="LightYear,LightYearAdmin,光年,后台模板,后台管理系统,光年HTML模板">
<meta name="description" content="Light Year Admin V5是一个基于Bootstrap v5.1.3的后台管理系统的HTML模板。">
<title>联动插件和select美化插件配合表单验证插件的使用 - 光年(Light Year Admin V4)后台管理系统模板</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="default">
<link rel="stylesheet" type="text/css" href="css/materialdesignicons.min.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="js/bootstrap-lyear-select/bootstrap-lyear-select.css">
<link rel="stylesheet" type="text/css" href="css/style.min.css">
<style>

</style>
</head>
  
<body>
<div class="container-fluid">

  <div class="row">

  	<div class="col-lg-12">
  	  <div class="card">
  	    <header class="card-header">
  	      <div class="card-title">联动插件和select美化插件配合表单验证插件的使用</div>
  	    </header>
  		<div class="card-body">
          
          <p>为了稍微美化下select，这里引入了插件bootstrap-lyear-select。</p>
  		  <form action="#!" method="post" id="example-from">
            <div class="row">
              <div class="col-md-12">
                <div class="mb-2">
                  <label class="form-label">请选择地区</label>
                  <div class="clearfix" id="example-1"></div>
                </div>
              </div>
              <div class="col-12">
                <button type="submit" class="btn btn-primary">确认提交</button>
              </div>
            </div>
          </form>
          
  	    </div>
  	  </div>
    </div>

  </div>

</div>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/popper.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/main.min.js"></script>
<script type="text/javascript" src="js/jquery-validate/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/jquery-validate/messages_zh.min.js"></script>
<script type="text/javascript" src="js/bootstrap-lyear-select/bootstrap-lyear-select.js"></script>
<script type="text/javascript" src="js/bootstrap-selectN.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    var catData = [{"id":1,"name":"周边旅游","children":[{"id":24,"name":"广东","status":0,"children":[{"id":7,"name":"广州"},{"id":23,"name":"潮州"}]}]},{"id":5,"name":"国内旅游","children":[{"id":8,"name":"华北地区","children":[{"id":9,"name":"北京"}]}]},{"id":6,"name":"出境旅游","children":[{"id":10,"name":"东南亚","children":[{"id":11,"name":"马来西亚","children":[{"id":20,"name":"沙巴","children":[{"id":21,"name":"美人鱼岛","children":[{"id":22,"name":"潜水"}]}]}]}]}]}];
    
    var catIns1 = $('#example-1').bootstrapSelectN({
        data: catData,
        name: ['select1', 'select2', 'select3', 'select4', 'select5', 'select6'], // 如果要做验证，这里给所有的select命名，避免无法验证
    }, function(obj) {
        obj.rules('add', {
            required: true,
            messages:{
                required: '这是必填字段',
            }
        }); // 这个通过回调函数追加新增select的验证
    });
    
    var validator = $('#example-from').validate({
        ignore: ".ignore",    // 插件默认不验证隐藏元素,这里可以自定义一个不验证的class,即验证隐藏元素,不验证class为.ignore的元素
        focusInvalid: false,  // 禁用无效元素的聚焦
        rules: {
            'select1': {
                required: true
            }, // 默认给第一个验证
        },
        errorPlacement: function errorPlacement(error, element) {
            $(element).addClass('is-invalid');
        },
        highlight: function(element) {
            var $el = $(element);
            if ($el.hasClass('selectN')) {
                $el.parent().find('.lyear-select > .form-control').addClass('is-invalid');
            }
        },
        unhighlight: function(element) {
            var $el = $(element);
            if ($el.hasClass('selectN')) {
                $el.parent().removeClass('is-invalid');
                var isInvalid = false;
                $('.selectN').each(function() {
                    if ($(this).parent().hasClass('is-invalid')) {
                        isInvalid = true;
                    }
                });
                if (!isInvalid) {
                    $el.removeClass('is-invalid');
                }
            } else {
                $el.removeClass('is-invalid');
            }
        },
        submitHandler: function(form) {
            alert('验证成功');
        }
    });
});
</script>
</body>
</html>